class_name PoisonArrowEffect
extends Effect

# 毒箭效果 - 模块化设计
# 功能：造成立即伤害，然后每回合造成毒伤

# 可配置参数
var immediate_damage: int = 8  # 立即伤害
var poison_damage: int = 3     # 每回合毒伤
var poison_duration: int = 3   # 毒伤持续时间

func execute(targets: Array[Node]) -> void:
	print("☠️ PoisonArrowEffect.execute() 被调用")
	
	for target in targets:
		if not target or not is_instance_valid(target):
			continue
		if target is Enemy:
			_apply_poison_arrow(target)
			if sound:
				SFXPlayer.play(sound)
		else:
			print("❌ 目标不是Enemy类型: %s" % target.get_class())

func _apply_poison_arrow(enemy: Enemy) -> void:
	print("☠️ 毒箭效果触发，敌人: %s" % enemy.name)
	
	# 1. 立即造成伤害
	_apply_immediate_damage(enemy)
	
	# 2. 调度毒伤效果
	_schedule_poison_damage(enemy)

func _apply_immediate_damage(enemy: Enemy) -> void:
	# 直接造成伤害
	enemy.take_damage(immediate_damage, Modifier.Type.DMG_TAKEN)
	print("💥 %s 受到 %d 点立即伤害" % [enemy.name, immediate_damage])

func _schedule_poison_damage(enemy: Enemy) -> void:
	# 创建毒伤调度效果
	var poison_effect = ScheduledEffect.new()
	poison_effect.id = "poison_arrow_damage_%s" % str(randi())
	poison_effect.trigger_type = ScheduledEffect.TriggerType.PHASE_SPECIFIC
	poison_effect.target_type = ScheduledEffect.TargetType.CUSTOM
	poison_effect.trigger_phase = TurnPhaseConstants.ENEMY_END  # 敌人回合结束时
	poison_effect.is_repeating = true
	poison_effect.repeat_interval = 1
	poison_effect.max_triggers = poison_duration
	
	# 创建毒伤效果
	var poison_damage_effect = PoisonDamageEffect.new()
	poison_damage_effect.amount = poison_damage
	poison_damage_effect.targets = [enemy]
	poison_damage_effect.sound = sound
	
	poison_effect.custom_effects.append(poison_damage_effect)
	
	# 添加到调度器
	var battle = enemy.get_tree().get_first_node_in_group("battle")
	if battle and battle.resource_scheduler:
		battle.resource_scheduler.add_scheduled_effect(poison_effect)
		print("☠️ 已调度毒伤效果：每回合造成 %d 点毒伤，持续 %d 回合" % [poison_damage, poison_duration])
	else:
		print("❌ 无法找到资源调度器")

# 配置方法 - 允许外部设置参数
func configure(immediate_dmg: int = 8, poison_dmg: int = 3, duration: int = 3) -> void:
	self.immediate_damage = immediate_dmg
	self.poison_damage = poison_dmg
	self.poison_duration = duration
	print("⚙️ PoisonArrowEffect 配置完成：")
	print("  - 立即伤害: %d" % immediate_damage)
	print("  - 毒伤: %d" % poison_damage)
	print("  - 持续: %d 回合" % poison_duration) 
